﻿using Cofe.Core.Property;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cofe.Core.Filter
{
    [EntryFilter("filetype")]
    public class FileTypeEntryFilter : EntryFilterBase
    {
        public const string File = "file";
        public const string Folder = "folder";

        public FileTypeEntryFilter(string type, string id = "filetype")
            : base(type.TrimStart('.').Replace("?", "").Replace("*", "")
            , id)
        {
            SupportedMatchType = MatchType.MatchThisEntry;
        }

        public override MatchType matches(IPropertyHost entry,
            params IOptionFilter[] options)
        {
            bool match = false;

            if (Value == FileTypeEntryFilter.Folder)
                match = entry.SupportedDefinitions.Contains(
                    Cofe.Core.PropertyDefinitions.CofeDirDefinitions,
                    PropertyDefinitionComparer.CompareCategory);
            else
                if (Value == FileTypeEntryFilter.File)
                    match = entry.SupportedDefinitions.Contains(
                        Cofe.Core.PropertyDefinitions.CofeFileDefinitions,
                        PropertyDefinitionComparer.CompareCategory);
                else
                {
                    string entryName = entry.Behaviors.GetProperty(CofeProperties.Name).ValueAsString;
                    string ext = PathFE.GetExtension(entryName).TrimStart('.');
                    match =                    
                        Value.Equals(ext,
                        StringComparison.CurrentCultureIgnoreCase) ||
                        Value.Equals(File, StringComparison.CurrentCultureIgnoreCase);
                }

            return match ? MatchType.MatchThisEntry : MatchType.None;
        }
    }
}
